package com.ecjtu.gateway.netty;

import com.alibaba.fastjson.JSON;
import com.ecjtu.gateway.netty.model.DasDataModel;
import com.ecjtu.gateway.netty.model.PacketDataModel;
import com.ecjtu.gateway.netty.util.HexUtil;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class NettyDecoder extends LengthFieldBasedFrameDecoder {
    public NettyDecoder() {
        super(Integer.MAX_VALUE, 6, 4, 4, 0);
    }
    public NettyDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
    }

    @Override
    public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        ByteBuf frame = null;

        try {
            frame = (ByteBuf) super.decode(ctx, in);
            if (null == frame) {
                return null;
            }
            PacketDataModel packetDataModel = new PacketDataModel();
            packetDataModel.setSyncHeader(frame.readInt());
            log.info("header = {}",Integer.toHexString(packetDataModel.getSyncHeader()));
            packetDataModel.setBodyType(frame.readShort());
            Integer bodyLength = frame.readInt();
            log.info(" bodyLen = {}", bodyLength);
            frame.readByte();
            DasDataModel dasDataModel = new DasDataModel();

            packetDataModel.setDasDataModel(dasDataModel);
            frame.readByte();
            frame.readInt();
            Integer pkId = frame.readUnsignedShort();
            dasDataModel.setPkId(pkId);
            dasDataModel.setStartChannel(frame.readInt());
            dasDataModel.setDataChannelCount(frame.readInt());
            dasDataModel.setPerPointerCount(frame.readInt());
            dasDataModel.setSampleFrequency(frame.readInt());
            dasDataModel.setChannelInterval(frame.readFloat());
            List<DasDataModel.DetailData> detailDataList = Lists.newArrayList();
            for (int i = 0; i < dasDataModel.getPerPointerCount(); i++) {
                for (int j = 0; j < dasDataModel.getDataChannelCount(); j++) {
                    DasDataModel.DetailData detailData = new DasDataModel.DetailData();
                    detailData.setChannelId(j);
                    detailData.setPointerId(i);
                    detailData.setData(frame.readShort());
                    detailDataList.add(detailData);
                }
            }
            dasDataModel.setDetailDataList(detailDataList);
            Short status = frame.readShort();
            Integer lastPacket =  frame.readInt();
            ctx.channel().write(pkId);

            log.info("lastPacket = {},status = {}",Integer.toHexString(lastPacket),status);
            return packetDataModel;
        } catch (Exception e) {
            log.error("decode exception, {}" , e);
        } finally {
            if (null != frame) {
                frame.release();
            }
        }

        return null;
    }
}
